package org.example.email_learn.controller;

import jakarta.annotation.Resource;
import lombok.Getter;
import org.example.email_learn.service.EmailService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/email")
public class EmailController {

    @Resource
    private EmailService emailService;

    @Resource
    private RedisTemplate redisTemplate;

    @PostMapping("/send")
    public void addOrders(@RequestParam String to,
                          @RequestParam String subject,
                          @RequestParam String text) {
        emailService.send(to,subject,text);

    }

    @GetMapping("/code")
    public String sendCode()
    {
        Random random = new Random();
        int code = random.nextInt(8999)+1000;
        String codeStr = "你的验证码" + code;
        redisTemplate.opsForValue().set("code",codeStr,10,TimeUnit.MINUTES);
        emailService.send("2465684844@qq.com","验证码" , codeStr);

        return "验证码已发送";
    }



}
